using System;
using System.Configuration;
using System.Runtime.Serialization;

namespace MDCAdminTool
{
    [Serializable]
    public class ClusterConfigElement : ConfigurationElement , ISerializable
    {
        // Fields
        internal const string CONNECTION_STR = "connectionString";
        internal const string CONNECTION_STR_DEFAULT = "";
        internal const string MAX_STORE_RETRIES = "maxRetries";
        internal const string PROVIDER = "provider";
        internal const string STORE_RETRY_INTERVAL = "retryInterval";
        internal const string XML_PROVIDER = "xml";

        // Methods
        internal ClusterConfigElement()
        {
        }

        protected ClusterConfigElement( SerializationInfo info , StreamingContext context )
        {
            this.Provider = info.GetString( "provider" );
            this.ConnectionString = info.GetString( "connectionString" );
            this.StoreRetryInterval = (int)info.GetValue( "retryInterval" , typeof( int ) );
            this.MaxStoreRetries = (int)info.GetValue( "maxRetries" , typeof( int ) );
        }

        internal ClusterConfigElement( string provider , string connectionStr )
        {
            this.Provider = provider;
            this.ConnectionString = connectionStr;
        }

        public void GetObjectData( SerializationInfo info , StreamingContext context )
        {
            info.AddValue( "provider" , this.Provider );
            info.AddValue( "connectionString" , this.ConnectionString );
            info.AddValue( "maxRetries" , this.MaxStoreRetries );
            info.AddValue( "retryInterval" , this.StoreRetryInterval );
        }

        // Properties
        [ConfigurationProperty( "connectionString" , DefaultValue="" , IsRequired=true )]
        internal string ConnectionString
        {
            get
            {
                return (string)base["connectionString"];
            }
            set
            {
                base["connectionString"] = value;
            }
        }

        internal ConnectionStringSettings ConnectionStringSettings
        {
            get
            {
                return new ConnectionStringSettings( this.Provider , this.ConnectionString , this.Provider );
            }
        }

        [ConfigurationProperty( "maxRetries" , DefaultValue=0x24 , IsRequired=false )]
        internal int MaxStoreRetries
        {
            get
            {
                return (int)base["maxRetries"];
            }
            set
            {
                base["maxRetries"] = value;
            }
        }

        [ConfigurationProperty( "provider" , DefaultValue="xml" , IsRequired=false )]
        internal string Provider
        {
            get
            {
                return (string)base["provider"];
            }
            set
            {
                base["provider"] = value;
            }
        }

        [ConfigurationProperty( "retryInterval" , DefaultValue=5 , IsRequired=false )]
        internal int StoreRetryInterval
        {
            get
            {
                return (int)base["retryInterval"];
            }
            set
            {
                base["retryInterval"] = value;
            }
        }
    }
}